package com.wwl.cms.utils.designModeTest.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author wwl
 * @version 1.0
 * @date 2021/9/8 19:35
 * @description:
 */
public class ProxyFactory implements MethodInterceptor {
    private Object target;

    public ProxyFactory(Object target) {
        this.target = target;
    }
    //返回一个代理对象：是target对象的代理对象
    public Object  getProxyInstance(){
        //1.创建一个工具类
        Enhancer enhancer = new Enhancer();
        //设置父类
        enhancer.setSuperclass(target.getClass());
        //3.设置回调函数
        enhancer.setCallback(this);
        //创建子类，即代理对象
        return enhancer.create();
    }
    @Override
    public Object intercept(Object arg0, Method method, Object[] args, MethodProxy arg3)throws Throwable{
        System.out.println("Cglib代理模式—————开始");
        Object returnVal = method.invoke(target,args);
        System.out.println("Cglib代理模式----提交");
        return returnVal;
    }
}
